package com.zhangqiancheng.wcdmaconsole.chart;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.view.ViewGroup;

public class BarChartViewGenerator {
    
    private static List<Double> data = new ArrayList<Double>();
    
    private static int[] xLabels = { 1, 2, 3, 4, 5, 6 };
    
    static {
        for (int i = 0; i < 6; i++)
            data.add(0.0);
    }

    public static void showBarChartView(ViewGroup view, double rssi) {

        data.remove(0);
        data.add(rssi);
        double[] value = new double[6];
        for (int i = 0; i < value.length; i++) { //.toArray(value);//{ 35, 25, 15, 30, 45, 20 };
            value[i] = data.get(i);
        }
        
        // double[] yLabels = { 0, 50 };

        view.addView(barChart(view.getContext(), "", value, Color.BLUE, 6, 5, new double[] { 0.5,
                6.5, -100, 0 },
                xLabels, "",
                true));

    }

    public static GraphicalView barChart(Context context, String title, double[] value, int color,
            int x, int y,
            double[] range,
            int[] xLable, String xTitle, boolean f) {

        // 多个渲染
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        // 多个序列的数据集
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        // 构建数据集以及渲染

        XYSeries series = new XYSeries(title);
        double[] yLable = value;
        for (int j = 0; j < yLable.length; j++) {
            series.add(xLable[j], yLable[j]);
        }
        dataset.addSeries(series);

        XYSeriesRenderer xyRenderer = new XYSeriesRenderer();
        // 设置颜色
        xyRenderer.setColor(color);
        // 设置点的样式
        xyRenderer.setPointStyle(PointStyle.SQUARE);
        // 将要绘制的点添加到坐标绘制中
        renderer.addSeriesRenderer(xyRenderer);

        // 设置x轴标签数
        renderer.setXLabels(x);
        // 设置Y轴标签数
        renderer.setYLabels(y);
        // 设置x轴的最大值
        renderer.setXAxisMax(x - 0.5);
        
        // 设置Y轴最小值和最大值
        renderer.setYAxisMin(-100);
        renderer.setYAxisMax(0);
        
        // 设置轴的颜色
        renderer.setAxesColor(Color.BLACK);
        // 设置x轴和y轴的标签对齐方式
        renderer.setXLabelsAlign(Align.CENTER);
        renderer.setYLabelsAlign(Align.RIGHT);
        // 设置现实网格
        renderer.setShowGrid(false);
        renderer.setShowLegend(false);
        renderer.setShowAxes(true);
        // 设置条形图之间的距离
        renderer.setBarSpacing(0.1);
        // renderer.setBarWidth(2);
        renderer.setInScroll(false);
        renderer.setPanEnabled(false, false);
        renderer.setClickEnabled(false);
        // 设置x轴和y轴标签的颜色
        // renderer.setXLabelsColor(Color.RED);
        // renderer.setYLabelsColor(0, Color.RED);
        renderer.setShowLabels(false);

        int length = renderer.getSeriesRendererCount();
        // 设置图标的标题
        renderer.setChartTitle(xTitle);
        renderer.setLabelsColor(Color.RED);

        // 设置图例的字体大小
        renderer.setLegendTextSize(18);
        // 设置x轴和y轴的最大最小值
        renderer.setRange(range);
        renderer.setMarginsColor(0x00888888);
        renderer.addYTextLabel(0, "-100");
        for (int i = 0; i < length; i++) {
            SimpleSeriesRenderer ssr = renderer.getSeriesRendererAt(i);
            ssr.setChartValuesTextAlign(Align.RIGHT);
            ssr.setChartValuesTextSize(12);
            ssr.setDisplayChartValues(f);
        }
        GraphicalView mChartView = ChartFactory.getBarChartView(context, dataset,
                renderer,
                Type.DEFAULT);

        return mChartView;

    }
}
